module com
{
    export class WebSocket
    {
        private _webSocket: egret.WebSocket;
        private _hasConnected: boolean = false;      //是否连接成功过
        private _connectCallBack: Function;
        private _thisObject: any;//回调对象
        private _isSelfClose: boolean = false;      //是否是自己主动关闭的socket的
        private _connectCloseBack: Function;
        private _connectErrorBack: Function;
        private _url: string;

        public constructor()
        {
            this._webSocket = new egret.WebSocket();
            this._webSocket.type = egret.WebSocket.TYPE_STRING;
            this._webSocket.addEventListener(egret.Event.CONNECT, this.onSocketConnected, this);
            this._webSocket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this);
            this._webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onDataIn, this);
            this._webSocket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onIoError, this);
        }

		/**
		 * 当socket连接上
		 */
        private onSocketConnected(evt: egret.Event): void
        {
            this._hasConnected = true;
            if (this._connectCallBack != null) {
                this._connectCallBack.call(this._thisObject);
            }
        }
		/**
         * 当socket关闭
         */
        private onSocketClose(evt: egret.Event): void
        {
            this._hasConnected = false;
            if (!this._isSelfClose) {
                //非主动关闭的才会抛出事件
                if (this._connectCloseBack != null) {
                    this._connectCloseBack.call(this._thisObject);
                }
            }
        }
        private send(): void
        {
            if (this._webSocket) 
            {
                this._webSocket.writeUTF('{11111}');
                this._webSocket.flush();
            }
        }
		/**
         * 接收数据返回
         */
        private onDataIn(evt: egret.ProgressEvent): void
        {
            if (this._webSocket) {
                let str = this._webSocket.readUTF();
                // let message = JSON.parse(str);
            }
        }
		/**
         * 当发生IO错误
         */
        private onIoError(evt: egret.IOErrorEvent): void
        {
            this._hasConnected = false;
            if (!this._isSelfClose) {
                if (this._connectErrorBack != null) {
                    this._connectErrorBack.call(this._thisObject);
                }
            }
        }
		/**
         * 连接指定服务器
         */
        public connect(host: string, port: number, thisObject: any, connectCallBack: Function, connectCloseBack: Function, connectErrorBack: Function): void
        {
            if (!this._webSocket.connected) {
                this._thisObject = thisObject;
                this._connectCallBack = connectCallBack;
                this._connectCloseBack = connectCloseBack;
                this._connectErrorBack = connectErrorBack;
                this._isSelfClose = false;
                this._webSocket.connect(host, port);
                this.debugConnect();
            }
        }
        /**
         * 以url的方式连接服务器
         */
        public connectByURL(url: string, thisObject: any, connectCallBack: Function, connectCloseBack: Function, connectErrorBack: Function): void
        {
            if (!this._webSocket.connected) {
                this._url = url;
                this._thisObject = thisObject;
                this._connectCallBack = connectCallBack;
                this._connectCloseBack = connectCloseBack;
                this._connectErrorBack = connectErrorBack;
                this._isSelfClose = false;
                this._webSocket.connectByUrl(url);
                this.debugConnect();
            }
        }
		/**
         * 开启debug调试
         */
        private debugConnect(): void
        {
            //TickManager.setTickOut(this.onConnectTimeOut, this, 10, TickManager.TIMER_TICK);
        }
    }
}